Page({
  data: {
    sortOptions: ['按提交时间倒序排列', '按提交时间正序排列'],
    sortIndex: 0,
    totalPages: 1,
    pages: 4,
    pageSize: 10,
    pageNum: 1,
    toPage: '',
    activities: [
    ],
    searchKeyword:'',
    aname:''
  },
  onLoad(){
     this.loadPage()
  },
  onConfirmSearch() {
    this.setData({
      pageNum:1,
      aname:this.data.searchKeyword
    })
    this.loadPage()
  },
  //搜索框文字的变化
  onInputChange(e) {
    this.setData({
      searchKeyword: e.detail.value
    })
  },
   // 分页相关方法
   onPrevPage() {
    if (this.data.pageNum > 1) {
      this.setData({
        pageNum: this.data.pageNum - 1
      });
      this.loadPage();
    }
  },

  onNextPage() {
    if (this.data.pageNum < this.data.pages) {
      this.setData({
        pageNum: this.data.pageNum + 1
      });
      this.loadPage();
    }
  },

  onPageInput(e) {
    const page = parseInt(e.detail.value);
    if (page >= 1 && page <= this.data.pages) {
      this.setData({
        toPage: page
      });
    }
  },
  onPageSearch() {
    this.setData({
      pageNum:this.data.toPage
    })
    this.loadPage();
  },

  loadPage() {
    const pageNum = this.data.pageNum
    const orderDirection = this.data.sortOptions[this.data.sortIndex]
    const aname = this.data.aname
    const app = getApp()
    app.activityApi.enableActivity({pageNum,orderDirection,aname}).then(res => {
      console.log(res.data.data)
      this.setData({
        activities:res.data.data.list,
        pageNum:res.data.data.pageNum,
        pages:res.data.data.pages
      })
    })
  },

  handleSortChange(e) {
    this.setData({
      sortIndex: e.detail.value,
      pageNum:1
    })
    this.loadPage()
    
  },

  handleStatusChange(e) {
    this.setData({
      statusIndex: e.detail.value
    })
    this.loadPage()
  },

  onCreateActivity() {
    wx.navigateTo({
      url: '/pages/activity/create/index'
    })
  },

  onPublishResult() {
    wx.navigateTo({
      url: '/pages/activity/submitResult'
    })
  },

  onPageInput(e) {
    this.setData({
      toPage: e.detail.value
    })
  },

  onJoinAddition(event) {
    const aid = event.currentTarget.dataset.aid
    wx.navigateTo({
      url: '/pages/activity/joinActivity?aid='+aid,
    })
  },
  onPullDownRefresh() {
    this.loadPage()
    setTimeout(() => {
      wx.stopPullDownRefresh()
      wx.showToast({
        title: '刷新成功',
        icon: 'success',
        duration: 1000
      })
    }, 1000)
  },
   // 新增页面变化处理函数
   handlePageChange(e) {
    const { pageNum } = e.detail;
    this.setData({
      pageNum
    });
    this.loadPage();
  },
})